package fr.pud.client.view.mapstation;

import javax.swing.ImageIcon;

import fr.pud.client.LaunchProgram;
import fr.pud.utils.Tools;

/**
 * This class implements the singleton pattern to be sure that only one
 * image of the pointer is going to be mapped in the memory. It enables to save
 * some memory for the other applications
 * @author simondevineau
 */
public class PointerBackground {
    public static final int          INDEX_START           = 1;
    public static final int          INDEX_END             = 2;
    public static final int          INDEX_INTERMEDIATE    = 3;
    /**
     * A icon of a pointer to signalize the address
     */
    private static PointerBackground singletonStart        = null;
    private static PointerBackground singletonEnd          = null;
    private static PointerBackground singletonIntermediate = null;
    /**
     * TThe image of the pointer
     */
    private static ImageIcon         imagePointerStart;
    private static ImageIcon         imagePointerEnd;
    private static ImageIcon         imagePointerIntermediate;

    /**
     * @return the height of the pointer
     */
    public static final int getIconHeight(int indexPointer) {
        initialization();
        if (indexPointer == INDEX_START) {
            return imagePointerStart.getIconHeight();
        }
        else {
            if (indexPointer == INDEX_END) {
                return imagePointerEnd.getIconHeight();
            }
            else {
                return imagePointerIntermediate.getIconHeight();
            }
        }
    }

    /**
     * @return the width of the pointer
     */
    public static final int getIconWidth(int indexPointer) {
        initialization();
        if (indexPointer == INDEX_START) {
            return imagePointerStart.getIconWidth();
        }
        else {
            if (indexPointer == INDEX_END) {
                return imagePointerEnd.getIconWidth();
            }
            else {
                return imagePointerIntermediate.getIconWidth();
            }
        }
    }

    /**
     * @return the unique imageIcon instantiated
     */
    public static final ImageIcon getImageIcon(int indexPointer) {
        initialization();
        if (indexPointer == INDEX_START) {
            return imagePointerStart;
        }
        else {
            if (indexPointer == INDEX_END) {
                return imagePointerEnd;
            }
            else {
                return imagePointerIntermediate;
            }
        }
    }

    /**
     * The method which tests if the singletonDepart has already been
     * implemented.
     */
    private static void initialization() {
        if (singletonStart == null) {
            singletonStart = new PointerBackground(INDEX_START);
            if (singletonEnd == null) {
                singletonEnd = new PointerBackground(INDEX_END);
                if (singletonIntermediate == null) {
                    singletonIntermediate = new PointerBackground(
                            INDEX_INTERMEDIATE);
                }
            }
        }
    }

    /**
     * The private constructor to be sure that no other instance can be
     * instantiated
     */
    private PointerBackground(int indexPointer) {
        try {
            if (indexPointer == INDEX_START) {
                imagePointerStart = new ImageIcon(
                        LaunchProgram.class
                                .getResource("images/addressPointerStart.png"));
            }
            else {
                if (indexPointer == INDEX_END) {
                    imagePointerEnd = new ImageIcon(
                            LaunchProgram.class
                                    .getResource("images/addressPointerEnd.png"));
                }
                else {
                    imagePointerIntermediate = new ImageIcon(
                            LaunchProgram.class
                                    .getResource("images/addressPointerIntermediate.png"));
                }
            }
        }
        catch (Exception e) {
            Tools.writeToLogFile("Error while pointer backGround",
                    Tools.LOG_FILE_NO_JOPTION_PANE, e.getStackTrace());
        }
    }
}
